package com.hkts.management.contract.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.management.contract.entity.HktsContract;
import com.hkts.management.contract.service.IHktsContractService;
import com.hkts.management.model.VO.HktsContractOnPCVo;
import com.hkts.management.model.result.ResultData;
import com.hkts.management.utils.CustomPageUtils;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hml
 * @since 2021-04-22
 */
@RestController
@RequestMapping("/order/hktsContract")
@Api(tags = "合同管理接口")
public class HktsContractController {
    private final Logger logger = LoggerFactory.getLogger(HktsContractController.class);

    @Autowired
    public IHktsContractService iHktsContractService;

    /**
     * 分页查询数据
     *
     * @param page         分页信息
     * @param hktsContract 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getHktsContractPageList")
    public Result<Object> getHontUserList(Page<HktsContract> page, HktsContract hktsContract) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iHktsContractService.selectPage(page, hktsContract)));
    }

    /**
     * 起草合同
     *
     * @param hktsContract 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsContractSave")
    public Result<Object> hktsContractSave( HktsContract hktsContract) {
        return iHktsContractService.updateContract(hktsContract);
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/hktsContractGetById")
    public Result<Object> hktsContractGetById(String id) {
        HktsContract byId = iHktsContractService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsContractDelete")
    public Result<Object> hontUserDelete(String id) {
        iHktsContractService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsContractBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iHktsContractService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }

    /**
     * 起草合同回显
     * @param orderId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getContractByOrderId")
    public Result<Object> getContractByOrderId(String orderId){
        Map<String, Object> contractByOrderId = iHktsContractService.getContractByOrderId(orderId);
        return ResultUtil.data(contractByOrderId);
    }

    /**
     * pc端根据订单id查询合同
     * @param orderId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getContractByOrderIdOnPC")
    @ApiOperation(value = "pc端根据订单id查询合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单Id", dataType = "String",required = true)
    })
    public Result<HktsContractOnPCVo> getContractByOrderIdOnPC(String orderId){
        HktsContractOnPCVo contractByOrderId = iHktsContractService.getContractByOrderIdOnPC(orderId);
        return ResultUtil.data(contractByOrderId);
    }
    /**
     * 根据订单id查询合同
     */
    @ApiOperation(value = "根据合同id查询合同加噶及车牌号")
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getPriceAndCarCode/{id}")
    public Result<Map<String,Object>> getPriceAndCarCode(@PathVariable String id){
        HktsContract byId = iHktsContractService.getById(id);
        Map<String,Object> map=new HashMap<>();
        if(byId!=null){
            map.put("price",byId.getCargoPrice());
            map.put("carCode",byId.getCarrierPlate());
        }
        return ResultUtil.data(map);
    }
}
